package views
{
	import away3d.test.Panel;
	
	import controll.DataManager;
	import controll.IDataManager;
	import controll.XMLDataManager;
	
	import events.DataManagerEvent;
	import events.IProjectMouseEventAction;
	import events.ProjectEventViewEvent;
	import events.ProjectorBoardEvent;
	
	import flash.events.Event;
	
	import module.ProjectEventModule;
	
	import mx.events.FlexEvent;
	import mx.events.ResizeEvent;
	import mx.modules.Module;
	
	import spark.components.Button;
	
	public class ProjectBoard extends Module implements IBoardAction
	{
		private var m_dataManager:IDataManager;
		private var m_date:Date;
		private var m_eventAction:IProjectMouseEventAction;
		private var m_selectedPanelList:Vector.<IProjectEventView>;
		public function ProjectBoard()
		{
			//TODO: implement function
			super();
//			this.addEventListener(FlexEvent.INITIALIZE, init_handler, false, 0, true);
			this.addEventListener(FlexEvent.CREATION_COMPLETE, init_handler, false, 0, true);
			this.addEventListener(ResizeEvent.RESIZE, handleModuleResize, false, 0, true);
		}
		public function get date():Date
		{
			return m_date;
		}
		public function set date($date:Date):void
		{
			m_date = $date;
			var event:ProjectorBoardEvent = new ProjectorBoardEvent(ProjectorBoardEvent.DATE_CHANGED);
			this.dispatchEvent(event);
		}
		public function get selectedEvents():Vector.<IProjectEventView>
		{
			return this.m_selectedPanelList;
		}
		
		public function selectSingleProjectView($projectView:IProjectEventView):void
		{
			var event:ProjectEventViewEvent = new ProjectEventViewEvent(ProjectEventViewEvent.SELECT);
			event.projectEventView = $projectView;
			this.eventManager.dispatchEvent(event);
		}
		private function init_handler(e:Event):void
		{
			m_dataManager = createDataManagerInstance();
			m_dataManager.addEventListener(DataManagerEvent.EVENT_PANELS_LOADED, handleProjectEventLoaded, false, 0, true);
			init();
			createBg();
			var event:ProjectorBoardEvent = new ProjectorBoardEvent(ProjectorBoardEvent.BOARD_INIT);
			this.dispatchEvent(event);
//			loadData();
		}
		public function init():void
		{
//			trace ("init @ ProjectBoard");
			m_selectedPanelList = new Vector.<IProjectEventView>();
		}
		public function createBg():void
		{
//			trace("createBg @ ProjectBoard");
		}
		public function loadData($startDate:Date, $daysCount:Number):void
		{
			m_dataManager.loadEventPanelList(this, $startDate, $daysCount);
		}
		public function createDataManagerInstance():IDataManager
		{
			return DataManager.createInstance("controll.XMLDataManager");
		}
		protected function set eventManager($eventManager:IProjectMouseEventAction):void
		{
			m_eventAction = $eventManager;
			
		}
		protected function get eventManager():IProjectMouseEventAction
		{
			return m_eventAction;
		}
		protected function handleProjectEventCreate($event:ProjectEventViewEvent):void
		{
			if (m_dataManager != null)
			{
				
				m_dataManager.insertEventPanel($event.projectEvent);
			}
			
		}
		protected function handleViewUpdateComplete($event:ProjectEventViewEvent):void
		{
			if (m_dataManager != null)
			{
				var projectEventModule:ProjectEventModule = $event.projectEvent;
				m_dataManager.updateEventPanel(projectEventModule);
			}
		}
		protected function handleViewSelected($event:ProjectEventViewEvent):void
		{
			var view:IProjectEventView = $event.projectEventView;
			if (view == null)
			{
				for (var i:Number = 0; i < this.selectedEvents.length; i++)
				{
					var target:ProjectEventView = this.selectedEvents[i] as ProjectEventView;
					target.selected = false;
				}
				this.m_selectedPanelList = new Vector.<IProjectEventView>();
				return;
			}
			if (!view.selected)
			{
				view.selected = true;
				for (var i:Number = 0; i < this.m_selectedPanelList.length; i++)
				{
					var target:ProjectEventView = this.m_selectedPanelList[i] as ProjectEventView;
					target.selected = false;
				}
				this.m_selectedPanelList = new Vector.<IProjectEventView>();
				this.m_selectedPanelList.push(view);
			}
		}
		protected function handleProjectEventLoaded($event:DataManagerEvent):void
		{
		}
		protected function handleModuleResize($event:ResizeEvent):void
		{
		}
	}
}